package com.ngovanhau.justsort.adapter;

import com.ngovanhau.justsort.R;
import com.ngovanhau.justsort.model.Level;
import com.ngovanhau.justsort.model.LevelSetting;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class LevelRow implements Row {
	private final Level level;
	private final LayoutInflater inflater;

	public LevelRow(LayoutInflater inflater, Level level) {
		this.level = level;
		this.inflater = inflater;
	}

	public View getView(View convertView, ViewGroup group) {
		ViewHolder holder;
		View view;
		
		if (convertView == null) {
			ViewGroup viewGroup = (ViewGroup) inflater.inflate(
					R.layout.listview_item_level_item, group, false);
			holder = new ViewHolder(
			(TextView) viewGroup.findViewById(R.id.lv_item_size),
					(TextView) viewGroup.findViewById(R.id.lv_item_time),
					(TextView) viewGroup.findViewById(R.id.lv_item_min));
			viewGroup.setTag(holder);

			view = viewGroup;
		} else {
			holder = (ViewHolder) convertView.getTag();
			view = convertView;
		}
		holder.sizeView.setText(level.getRow() + "x" + level.getColumn());
		holder.timeView.setText( Integer.toString(level.getTime()));

		if (level.getType() == LevelSetting.unlockType) {
			holder.timeView.setVisibility(View.INVISIBLE);
			holder.labelView.setVisibility(View.INVISIBLE);
		}else{
			holder.timeView.setVisibility(View.VISIBLE);
			holder.labelView.setVisibility(View.VISIBLE);
		}

		return view;
	}

	public int getViewType() {
		return RowType.ITEM.ordinal();
	}

	private static class ViewHolder {
		final TextView sizeView;
		final TextView timeView;
		final TextView labelView;

		private ViewHolder(TextView sizeView,
				TextView timeView, TextView labelView) {
			this.sizeView = sizeView;
			this.timeView = timeView;
			this.labelView = labelView;
		}
	}
}
